<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="采购订单" prop="linkHeadId">
        <el-select v-model="queryParams.headerId" clearable filterable placeholder="请选择">
          <el-option
            v-for="item in allPurchaseList"
            :key="item.id"
            :label="item.number"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="项目代号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目代号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="合同编号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品名" prop="inventoryName">
        <el-input
          v-model="queryParams.inventoryName"
          placeholder="请输入商品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="inventorySpecifications">
        <el-input
          v-model="queryParams.inventorySpecifications"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="wbs" prop="wbs">-->
<!--        <el-input-->
<!--          v-model="queryParams.wbs"-->
<!--          placeholder="请输入wbs"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货状态" prop="shippingStatus">
        <el-select
          v-model="queryParams.shippingStatus"
          placeholder="发货状态"
          clearable
          style="width: 170px"
        >
          <el-option
            v-for="dict in dict.type.shipping_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="primary"-->
      <!--    plain-->
      <!--    icon="el-icon-plus"-->
      <!--    size="mini"-->
      <!--    @click="handleAdd"-->
      <!--    v-hasPermi="['datareport:ryDepotItems:add']"-->
      <!--  >新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="success"-->
      <!--    plain-->
      <!--    icon="el-icon-edit"-->
      <!--    size="mini"-->
      <!--    :disabled="single"-->
      <!--    @click="handleUpdate"-->
      <!--    v-hasPermi="['datareport:ryDepotItems:edit']"-->
      <!--  >修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="danger"-->
      <!--    plain-->
      <!--    icon="el-icon-delete"-->
      <!--    size="mini"-->
      <!--    :disabled="multiple"-->
      <!--    @click="handleDelete"-->
      <!--    v-hasPermi="['datareport:ryDepotItems:remove']"-->
      <!--  >删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datareport:ryDepotItems:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ryDepotItemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键" align="center" prop="id" />-->
      <!--<el-table-column label="表头Id" align="center" prop="headerId" />-->
      <el-table-column label="订单号" align="center" prop="number" />
      <el-table-column label="合同编号" align="center" prop="contractNumber"/>
      <el-table-column label="项目代号" align="center" prop="projectCode" />
      <el-table-column label="商品名" align="center" prop="inventoryName" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="规格" align="center" prop="inventorySpecifications" />
      <!--<el-table-column label="供应商id" align="center" prop="supplyId" />-->
      <el-table-column label="wbs" align="center" prop="wbs" />
      <el-table-column label="待采购数量" align="center" prop="advancePurchaseNum" />
<!--      <el-table-column label="总库可用数量" align="center" prop="usableNum" />-->
      <el-table-column label="实际采购数量" align="center" prop="operNumber" />
      <el-table-column label="发货状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shipping_status" :value="scope.row.shippingStatus"/>
        </template>
<!--        <template slot-scope="scope">-->
<!--          <el-select v-model="scope.row.shippingStatus" placeholder="请选择发货状态">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.shipping_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--          &lt;!&ndash;                <span>{{scope.row.name}}</span>&ndash;&gt;-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--  <template slot-scope="scope">-->
      <!--    <el-button-->
      <!--      size="mini"-->
      <!--      type="text"-->
      <!--      icon="el-icon-edit"-->
      <!--      @click="handleUpdate(scope.row)"-->
      <!--      v-hasPermi="['datareport:ryDepotItems:edit']"-->
      <!--    >修改</el-button>-->
      <!--    <el-button-->
      <!--      size="mini"-->
      <!--      type="text"-->
      <!--      icon="el-icon-delete"-->
      <!--      @click="handleDelete(scope.row)"-->
      <!--      v-hasPermi="['datareport:ryDepotItems:remove']"-->
      <!--    >删除</el-button>-->
      <!--  </template>-->
      <!--</el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单据子（出入库记录）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRyDepotItems, getRyDepotItems, delRyDepotItems, addRyDepotItems, updateRyDepotItems } from "@/api/datareport/ryDepotItems";
import {getAllList} from "@/api/purchase/purchaseOrder";

export default {
  name: "RyDepotItems",
  dicts: ['project_status', 'shipping_status', 'purchase_status'],
  data() {
    return {
      allPurchaseList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单据子（出入库记录）表格数据
      ryDepotItemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inventoryName: null,
        inventorySpecifications: null,
        supply: null,
        wbs: null,
        brand: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inventoryId: [
          { required: true, message: "商品Id不能为空", trigger: "blur" }
        ],
        supply: [
          { required: true, message: "供应商不能为空", trigger: "change" }
        ],
        wbs: [
          { required: true, message: "wbs不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAllPurchaseList();
  },
  methods: {
    /**查询所有采购订单列表*/
    getAllPurchaseList(){
      getAllList().then(response => {
        this.allPurchaseList = response.data;
      });
    },

    /** 查询单据子（出入库记录）列表 */
    getList() {
      this.loading = true;
      listRyDepotItems(this.queryParams).then(response => {
        this.ryDepotItemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        headerId: null,
        inventoryId: null,
        number: null,
        inventoryName: null,
        inventorySpecifications: null,
        supplyId: null,
        supply: null,
        wbs: null,
        advancePurchaseNum: null,
        usableNum: null,
        operNumber: null,
        unitPrice: null,
        allPrice: null,
        qualifiedNum: null,
        unqualifiedNum: null,
        unqualifiedReason: null,
        purchaseInId: null,
        purchaseOutId: null,
        remark: null,
        depotId: null,
        anotherDepotId: null,
        headerType: null,
        lockStatus: "0",
        taxRate: null,
        taxMoney: null,
        taxLastMoney: null,
        snList: null,
        batchNumber: null,
        expirationDate: null,
        operTime: null,
        operMan: null,
        linkId: null,
        tenantId: null,
        delFlag: null,
        belongtoProject: null,
        shippingStatus: "0",
        returnStatus: "0",
        retransmissionStatus: "0",
        retransmissionNum: null,
        location: null,
        isCreateOrder: null,
        deliveryTime: null,
        brand: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单据子（出入库记录）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRyDepotItems(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单据子（出入库记录）";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRyDepotItems(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRyDepotItems(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除单据子（出入库记录）编号为"' + ids + '"的数据项？').then(function() {
        return delRyDepotItems(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datareport/ryDepotItems/export', {
        ...this.queryParams
      }, `ryDepotItems_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
